package com.ph.irp.stock.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.base.currency.service.CurrencyService;
import com.ph.irp.base.verifyproject.service.PBaseVerifyprojectCandiateService;
import com.ph.irp.base.verifyproject.service.PBaseVerifyprojectDepartmentService;
import com.ph.irp.base.verifyproject.service.PBaseVerifyprojectSupplierService;
import com.ph.irp.dto.StockInvoiceDTO;
import com.ph.irp.model.PStockStockinvoice;
import com.ph.irp.model.PStockStockinvoicedetail;
import com.ph.irp.stock.service.StockInvoiceService;

public class StockInvoiceAction extends ActionSupport{
	private String FORWARD = null;
	private PStockStockinvoice stockInvoiceModel;
	private List<StockInvoiceDTO> stockInvoiceDTOList;
	private PStockStockinvoicedetail stockInvoiceDetailModel;
	private StockInvoiceService stockInvoiceService;
	private PBaseVerifyprojectSupplierService supplierService;
	private CurrencyService currencyService;
	private PBaseVerifyprojectCandiateService candiateService;
	private PBaseVerifyprojectDepartmentService departmentService;
	
	private String[] materialId;
	private String[] measureUnitId;
	private String[] basicUnitAmount;
	private String[] amount;
	private String[] unitPrice;
	private String[] totalUnitPrice;
	private String[] dutyFrank;
	private String[] discount;
	private String[] realTotalUnitPrice;
	private String[] money;
	private String[] moneyBw;
	private String[] discountPrice;
	private String[] discountPriceBw;
	private String[] dutyPrice;
	private String[] dutyPriceBw;
	private String[] totalMoney;
	private String[] totalMoneyBw;
	private String[] remark;	

	public PStockStockinvoice getStockInvoiceModel() {
		return stockInvoiceModel;
	}

	public void setStockInvoiceModel(PStockStockinvoice stockInvoiceModel) {
		this.stockInvoiceModel = stockInvoiceModel;
	}


	public PStockStockinvoicedetail getStockInvoiceDetailModel() {
		return stockInvoiceDetailModel;
	}

	public void setStockInvoiceDetailModel(
			PStockStockinvoicedetail stockInvoiceDetailModel) {
		this.stockInvoiceDetailModel = stockInvoiceDetailModel;
	}

	/*
	 * 打开新增页面
	 */
	public String showAddStockInvoice() {
		stockInvoiceModel = null;
		stockInvoiceDetailModel = null;
		FORWARD = "success";
		return FORWARD;
	}
	
	/*
	 * 新增
	 */
	public String addStockInvoice() throws Throwable{
		HttpSession session = ServletActionContext.getRequest().getSession();
		String username=(String)session.getAttribute("userName");
		FORWARD = "success";
		List<PStockStockinvoicedetail> stockInvoiceDetailList = new ArrayList<PStockStockinvoicedetail>();
		for(int i=0;i<materialId.length;i++){
			PStockStockinvoicedetail sid = new PStockStockinvoicedetail();
			sid.setMaterialId(Integer.valueOf(materialId[i]));
			sid.setBasicUnitAmount(Float.valueOf(basicUnitAmount[i]));
			sid.setMeasureUnitId(Integer.valueOf(measureUnitId[i]));
			sid.setAmount(Float.valueOf(amount[i]));
			sid.setUnitPrice(Float.valueOf(unitPrice[i]));
			sid.setDiscount(Float.valueOf(discount[i]));
			sid.setDutyFrank(Float.valueOf(dutyFrank[i]));
			sid.setTotalUnitPrice(Float.valueOf(totalUnitPrice[i]));
			sid.setRealTotalUnitPrice(Float.valueOf(realTotalUnitPrice[i]));
			sid.setMoney(Float.valueOf(money[i]));
			sid.setMoneyBw(Float.valueOf(moneyBw[i]));
			sid.setDiscountPrice(Float.valueOf(discountPrice[i]));
			sid.setDiscountPriceBw(Float.valueOf(discountPriceBw[i]));
			sid.setDutyPrice(Float.valueOf(dutyPrice[i]));
			sid.setDutyPriceBw(Float.valueOf(dutyPriceBw[i]));
			sid.setTotalMoney(Float.valueOf(totalMoney[i]));
			sid.setTotalMoneyBw(Float.valueOf(totalMoneyBw[i]));
			sid.setRemark(String.valueOf(remark[i]));
			System.out.println("1"+sid.getRemark()+"2");
			stockInvoiceDetailList.add(sid);
		}
		stockInvoiceModel.setMakeBiller(username);
		stockInvoiceModel.setIsAuditing(0);
		stockInvoiceModel.setIsCancel(0);
		stockInvoiceModel.setIsHook(0);
		stockInvoiceModel.setBillType(1);
		stockInvoiceService.addStockInvoice(stockInvoiceModel, stockInvoiceDetailList);
		return FORWARD;
	}
	
	/*
	 * 查找所有发票
	 */
	public String showStockInvoice() throws Throwable{
		stockInvoiceDTOList = new ArrayList<StockInvoiceDTO>();
		stockInvoiceModel = null;
		List<PStockStockinvoice> stockInvoiceList = stockInvoiceService.getAllStockInvoiceList();
		Iterator it = stockInvoiceList.iterator();
		while(it.hasNext()){
			StockInvoiceDTO stockInvoiceDTO = new StockInvoiceDTO();
			PStockStockinvoice sim = (PStockStockinvoice) it.next();
			stockInvoiceDTO.setStockInvoiceModel(sim);
			
			stockInvoiceDTO.setSupplierName(supplierService.getSupplierById(sim.getSupplierId()).getSupplierName());
			
			stockInvoiceDTO.setCurrencyName(currencyService.getCurrencyByID(sim.getCurrencyId()).getCurrencyName());
			
			if(sim.getManagerId()!=0)
				stockInvoiceDTO.setManager(candiateService.getCandiateById(sim.getManagerId()).getCandiate().getCandiateName());
		
			if(sim.getBusinessId()!=0)
				stockInvoiceDTO.setBusinesser(candiateService.getCandiateById(sim.getBusinessId()).getCandiate().getCandiateName());
			
			if(sim.getDepartmentId()!=0)
			stockInvoiceDTO.setDepartmentName(departmentService.getDepartmentById(sim.getDepartmentId()).getDepartment().getDepartmentName());
			
			stockInvoiceDTOList.add(stockInvoiceDTO);
		}
		FORWARD = "success";
		return FORWARD;
	}
	/*
	 * 条件查询发票
	 */
	public String getStockInvoiceList() throws Throwable{
		stockInvoiceDTOList = new ArrayList<StockInvoiceDTO>();
		List<PStockStockinvoice> stockInvoiceList = stockInvoiceService.getStockInvoiceList(stockInvoiceModel);
		Iterator it = stockInvoiceList.iterator();
		while(it.hasNext()){
			StockInvoiceDTO stockInvoiceDTO = new StockInvoiceDTO();
			PStockStockinvoice sim = (PStockStockinvoice) it.next();
			stockInvoiceDTO.setStockInvoiceModel(sim);
			
			stockInvoiceDTO.setSupplierName(supplierService.getSupplierById(sim.getSupplierId()).getSupplierName());
			
			stockInvoiceDTO.setCurrencyName(currencyService.getCurrencyByID(sim.getCurrencyId()).getCurrencyName());
			
			if(sim.getManagerId()!=0)
				stockInvoiceDTO.setManager(candiateService.getCandiateById(sim.getManagerId()).getCandiate().getCandiateName());
		
			if(sim.getBusinessId()!=0)
				stockInvoiceDTO.setBusinesser(candiateService.getCandiateById(sim.getBusinessId()).getCandiate().getCandiateName());
			
			if(sim.getDepartmentId()!=0)
			stockInvoiceDTO.setDepartmentName(departmentService.getDepartmentById(sim.getDepartmentId()).getDepartment().getDepartmentName());
			
			stockInvoiceDTOList.add(stockInvoiceDTO);
		}
		stockInvoiceModel = null;
		FORWARD = "success";
		return FORWARD;
	}
	
	public StockInvoiceService getStockInvoiceService() {
		return stockInvoiceService;
	}

	public void setStockInvoiceService(StockInvoiceService stockInvoiceService) {
		this.stockInvoiceService = stockInvoiceService;
	}

	public String getFORWARD() {
		return FORWARD;
	}

	public void setFORWARD(String forward) {
		FORWARD = forward;
	}

	public String[] getMaterialId() {
		return materialId;
	}

	public void setMaterialId(String[] materialId) {
		this.materialId = materialId;
	}

	public String[] getMeasureUnitId() {
		return measureUnitId;
	}

	public void setMeasureUnitId(String[] measureUnitId) {
		this.measureUnitId = measureUnitId;
	}

	public String[] getBasicUnitAmount() {
		return basicUnitAmount;
	}

	public void setBasicUnitAmount(String[] basicUnitAmount) {
		this.basicUnitAmount = basicUnitAmount;
	}

	public String[] getAmount() {
		return amount;
	}

	public void setAmount(String[] amount) {
		this.amount = amount;
	}

	public String[] getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(String[] unitPrice) {
		this.unitPrice = unitPrice;
	}

	public String[] getTotalUnitPrice() {
		return totalUnitPrice;
	}

	public void setTotalUnitPrice(String[] totalUnitPrice) {
		this.totalUnitPrice = totalUnitPrice;
	}

	public String[] getDutyFrank() {
		return dutyFrank;
	}

	public void setDutyFrank(String[] dutyFrank) {
		this.dutyFrank = dutyFrank;
	}

	public String[] getDiscount() {
		return discount;
	}

	public void setDiscount(String[] discount) {
		this.discount = discount;
	}

	public String[] getRealTotalUnitPrice() {
		return realTotalUnitPrice;
	}

	public void setRealTotalUnitPrice(String[] realTotalUnitPrice) {
		this.realTotalUnitPrice = realTotalUnitPrice;
	}

	public String[] getMoney() {
		return money;
	}

	public void setMoney(String[] money) {
		this.money = money;
	}

	public String[] getMoneyBw() {
		return moneyBw;
	}

	public void setMoneyBw(String[] moneyBw) {
		this.moneyBw = moneyBw;
	}

	public String[] getDiscountPrice() {
		return discountPrice;
	}

	public void setDiscountPrice(String[] discountPrice) {
		this.discountPrice = discountPrice;
	}

	public String[] getDiscountPriceBw() {
		return discountPriceBw;
	}

	public void setDiscountPriceBw(String[] discountPriceBw) {
		this.discountPriceBw = discountPriceBw;
	}

	public String[] getDutyPrice() {
		return dutyPrice;
	}

	public void setDutyPrice(String[] dutyPrice) {
		this.dutyPrice = dutyPrice;
	}

	public String[] getDutyPriceBw() {
		return dutyPriceBw;
	}

	public void setDutyPriceBw(String[] dutyPriceBw) {
		this.dutyPriceBw = dutyPriceBw;
	}

	public String[] getTotalMoney() {
		return totalMoney;
	}

	public void setTotalMoney(String[] totalMoney) {
		this.totalMoney = totalMoney;
	}

	public String[] getTotalMoneyBw() {
		return totalMoneyBw;
	}

	public void setTotalMoneyBw(String[] totalMoneyBw) {
		this.totalMoneyBw = totalMoneyBw;
	}

	public String[] getRemark() {
		return remark;
	}

	public void setRemark(String[] remark) {
		this.remark = remark;
	}

	public List<StockInvoiceDTO> getStockInvoiceDTOList() {
		return stockInvoiceDTOList;
	}

	public void setStockInvoiceDTOList(List<StockInvoiceDTO> stockInvoiceDTOList) {
		this.stockInvoiceDTOList = stockInvoiceDTOList;
	}

	public PBaseVerifyprojectSupplierService getSupplierService() {
		return supplierService;
	}

	public void setSupplierService(PBaseVerifyprojectSupplierService supplierService) {
		this.supplierService = supplierService;
	}

	public CurrencyService getCurrencyService() {
		return currencyService;
	}

	public void setCurrencyService(CurrencyService currencyService) {
		this.currencyService = currencyService;
	}

	public PBaseVerifyprojectCandiateService getCandiateService() {
		return candiateService;
	}

	public void setCandiateService(PBaseVerifyprojectCandiateService candiateService) {
		this.candiateService = candiateService;
	}

	public PBaseVerifyprojectDepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(
			PBaseVerifyprojectDepartmentService departmentService) {
		this.departmentService = departmentService;
	}
}
